import os,getpass
import time,re,win32ui
import public,language_chs
import tkinter,tkinter.ttk,tkinter.messagebox,tkinter.filedialog,windnd
import threading

LOGO_path = public.resource_path(os.path.join('icon', 'android.ico'))
flow_package = public.resource_path(os.path.join('temp','flow_package.txt'))
flow_package_log = public.resource_path(os.path.join('temp','flow_package_log.txt'))
devices_type_log = public.resource_path(os.path.join('temp','devices_type_log.txt'))  # 记录设备类型
username = getpass.getuser()
make_dir = 'C:\\Users\\' + username + '\\Documents\\DA_Tools\\'
make_dir_s = make_dir + 'adb_test_temp\\'
path_page = make_dir_s + 'path_page\\'
# 调试日志
adb_debug_log = make_dir_s + 'debug.log'
# 实时保存设备序列号
devices_log = make_dir_s + 'devices.log'
# md5和大小路径保存
md5_size_path = path_page + 'md5_size_path.log'
# push状态本地路径保存
push_local_path1 = path_page + 'push_local1.log'
# push状态设备路径保存
push_device_path1 = path_page + 'push_device1.log'
# pull状态本地路径保存
pull_local_path1 = path_page + 'pull_local1.log'
# pull状态设备路径保存
pull_device_path1 = path_page + 'pull_device1.log'
# 保存文件实时大小的文件内容
ls_file_info = path_page + 'push_ls_file_info.log'
# 存储push的结果状态
push_result_log = path_page + 'push_result_log.log'
# 判断当前包名是否一致标记
package_flag = False
# 判断当前设备类型是否更换标记
devices_type_flag = False
# 判断当前push和pull的状态
push_pull_flag = False
if not os.path.exists(path_page):
    os.makedirs(path_page)
# 初始化语言对象
languages = language_chs.Language()
languages_all = languages.all_languages()


class Context:
    """接收新的页面按钮渲染"""


def flow_page_state(flow_button,flow_button_disable):
    # 更新新的页面渲染
    setattr(Context,"flow_button",flow_button)
    setattr(Context,"flow_button_disable",flow_button_disable)


def md5_page_state(md5_size_button,md5_size_button_disable):
    # 更新新的页面渲染
    setattr(Context,"md5_size_button",md5_size_button)
    setattr(Context,"md5_size_button_disable",md5_size_button_disable)


# 查询应用流量值工具界面
class Flow_Screen(object):
    def flow_form(self,flow_Button,flow_Button_disable,adb_test_disable,page_state_list,adb_test_close):
        self.flow_root = tkinter.Toplevel()
        self.flow_root.title(languages_all['flow_tools_name'])
        # screenWidth = self.screen_root.winfo_screenwidth()
        # screenHeight = self.screen_root.winfo_screenheight()
        w = 400
        h = 300
        # x = (screenWidth - w) / 2
        # y = (screenHeight - h) / 2
        self.flow_root.geometry('%dx%d' % (w, h))
        self.flow_root.iconbitmap(LOGO_path)
        self.flow_root.resizable(0, 0)
        # self.screen_root.wm_attributes('-topmost', 1)

        # with open(page_state_path,'w') as fp:

        self.flow_Button = flow_Button
        self.flow_Button_disable = flow_Button_disable
        self.adb_test_disable = adb_test_disable
        self.adb_test_close = adb_test_close
        self.page_state_list = page_state_list

        self.page_state_list.append('flow')
        self.flow_Button.place_forget()
        self.flow_Button_disable.place(x=20, y=20)
        self.adb_test_disable.place(x=400, y=370)

        self.flow_root.protocol('WM_DELETE_WINDOW',self.close_handle)
        self.main_frame()

        return self.flow_root

    def close_handle(self):
        # 监听页面消失
        self.page_state_list.remove('flow')
        self.flow_Button_disable.place_forget()
        self.flow_Button.place(x=20, y=20)

        if not self.page_state_list:
            self.adb_test_disable.place_forget()
            self.adb_test_close.place(x=400, y=370)
        try:
            getattr(Context, "flow_button_disable").place_forget()
            getattr(Context, "flow_button").place(x=20, y=20)
        except Exception:
            pass
        self.flow_root.destroy()

    def main_frame(self):
        # 上行流量与下行流量Label
        self.up_flow_label = tkinter.Label(self.flow_root,text=languages_all['upstream_flow'])
        self.up_flow_label.place(x=20,y=20)

        self.down_flow_label = tkinter.Label(self.flow_root, text=languages_all['downstream_flow'])
        self.down_flow_label.place(x=250, y=20)

        # 上行流量与下行流量显示框
        self.up_flow_str = tkinter.StringVar()
        self.up_flow = tkinter.Label(self.flow_root,textvariable=self.up_flow_str,bg='#FFFFFF',height=2,width=10,
                                     font=('宋体',15))
        self.up_flow.place(x=20,y=50)
        self.up_flow_str.set('0')

        self.down_flow_str = tkinter.StringVar()
        self.down_flow = tkinter.Label(self.flow_root, textvariable=self.down_flow_str, bg='#FFFFFF', height=2, width=10,
                                     font=('宋体',15))
        self.down_flow.place(x=250, y=50)
        self.down_flow_str.set('0')

        # 上行总流量与下行总流量label
        self.up_flow_total_label = tkinter.Label(self.flow_root, text=languages_all['upstream_all_flow'])
        self.up_flow_total_label.place(x=130, y=0)

        self.down_flow_total_label = tkinter.Label(self.flow_root, text=languages_all['downstream_all_flow'])
        self.down_flow_total_label.place(x=130, y=50)

        # 上行总流量与下行总流量显示框
        self.up_flow_total_str = tkinter.StringVar()
        self.up_flow_total = tkinter.Label(self.flow_root, textvariable=self.up_flow_total_str, bg='#FFFFFF', height=2, width=15,
                                     font=('宋体', 10))
        self.up_flow_total.place(x=130, y=20)
        self.up_flow_total_str.set('0')

        self.down_flow_total_str = tkinter.StringVar()
        self.down_flow_total = tkinter.Label(self.flow_root, textvariable=self.down_flow_total_str, bg='#FFFFFF', height=2,
                                       width=15,
                                       font=('宋体', 10))
        self.down_flow_total.place(x=130, y=70)
        self.down_flow_total_str.set('0')

        # 启动流量检测按钮
        self.start_button = tkinter.Button(self.flow_root,width=12,text=languages_all['flow_start'])
        self.start_button_disbale = tkinter.Button(self.flow_root,width=12,text=languages_all['flow_doing'])
        self.start_button_disbale.config(state='disable')
        self.start_button.bind('<Button-1>',lambda x:self.flow_main())
        self.start_button.place(x=30,y=102)

        # 暂停流量检测按钮
        self.stop_button = tkinter.Button(self.flow_root, width=12, text=languages_all['flow_stop'])
        self.stop_button_disbale = tkinter.Button(self.flow_root, width=12, text=languages_all['flow_stop'])
        self.stopping_button_disbale = tkinter.Button(self.flow_root, width=12, text=languages_all['flow_stop_doing'])
        self.stop_button_disbale.config(state='disable')
        self.stopping_button_disbale.config(state='disable')
        self.stop_button.bind('<Button-1>', lambda x: self.flow_stop_bind())
        self.stop_button_disbale.place(x=240, y=102)

        # 日志记录显示
        self.flow_frame = tkinter.Frame(self.flow_root,width=400,height=150)
        self.flow_scrollbar = tkinter.Scrollbar(self.flow_frame)
        self.flow_text = tkinter.Text(self.flow_frame,yscrollcommand=(self.flow_scrollbar.set),width=50,height=12,
                                      font=('宋体',10))
        self.flow_scrollbar.config(command=self.flow_text.yview)
        self.flow_scrollbar.pack(side=(tkinter.RIGHT),fill=(tkinter.Y))
        self.flow_text.config(state='disable')  # 设为disable防止Text多行文本框能被点击
        self.flow_text.pack()
        self.flow_frame.place(x=20,y=135)

    def flow_stop_bind(self):
        def t_flow_stop():
            # 停止检测流量
            global flow_stop_flag
            self.stopping_button_disbale.place(x=240,y=102)
            self.flow_text.config(state='normal')
            self.flow_text.insert(tkinter.END, languages_all['flow_stoped'])
            self.flow_text.see(tkinter.END)
            self.flow_text.config(state='disable')
            flow_stop_flag = True
            time.sleep(2)  # 延时设置避免未重置数值
            self.up_flow_str.set('0KB/s')
            self.down_flow_str.set('0KB/s')
            self.up_flow_total_str.set('0')
            self.down_flow_total_str.set('0')
            return flow_stop_flag

        t_flow_stop = threading.Thread(target=t_flow_stop)
        t_flow_stop.setDaemon = True
        t_flow_stop.start()

    @public.adb_tools_log
    def flow_main(self):
        def flow_text_normal():
            self.flow_text.config(state='normal')

        def flow_text_disable():
            self.flow_text.config(state='disable')

        def get_pid():
            global package_flag
            # 获取包名及Uid
            # 获取当前应用的包名
            time.sleep(1)
            flow_text_normal()
            device = open(devices_log,'r').read()
            package_name = public.found_packages(device)
            # print(package_name)
            if not package_name:
                self.flow_text.insert(tkinter.END, languages_all['flow_find_packages_fail'])
                self.flow_text.see(tkinter.END)
                flow_text_disable()
                package_flag = False
            else:
                with open(flow_package, 'w') as fp:
                    fp.write(package_name.strip())
                fp.close()
                self.flow_text.insert(tkinter.END, languages_all['flow_find_packages_success'] + package_name + '\n')
                self.flow_text.see(tkinter.END)
                flow_text_disable()
                # 获取对应的包名的pid
                public.execute_cmd('adb -s ' + device + ' shell ps > ' + flow_package_log)
                ps_result_finally = open(flow_package_log, 'r').read()
                pid_result_re = re.findall('\n\w+.*?(\d+).*?' + package_name + '\n', ps_result_finally)
                pid_result = ''.join(pid_result_re)
                if pid_result == '':
                    flow_text_normal()
                    self.flow_text.insert(tkinter.END, languages_all['flow_find_pid_fail'])
                    self.flow_text.see(tkinter.END)
                    flow_text_disable()
                    package_flag = False
                else:
                    flow_text_normal()
                    self.flow_text.insert(tkinter.END, package_name + languages_all['flow_find_pid'] + pid_result + '\n')
                    self.flow_text.see(tkinter.END)
                    # flow_text_disable()
                    # # 获取包名的uid
                    # uid_result_status = public.execute_cmd('adb -s ' + device + ' shell cat /proc/' + pid_result + '/status')
                    # uid_result_re = re.findall('Uid.*?(\d+).*?', uid_result_status)
                    # uid_result = ''.join(uid_result_re)
                    # flow_text_normal()
                    # self.flow_text.insert(tkinter.END, package_name + '获取的Uid：' + uid_result + '\n')
                    # self.flow_text.see(tkinter.END)
                    self.flow_text.insert(tkinter.END, languages_all['flow_count_doing'])
                    self.flow_text.see(tkinter.END)
                    flow_text_disable()
                    return pid_result

        def t_flow_main():
            global package_flag,devices_type_flag,flow_stop_flag
            flow_stop_flag = False  # 暂停标识
            # 查询流量主要逻辑
            self.start_button_disbale.place(x=30,y=102)
            self.stop_button_disbale.place_forget()
            self.stop_button.place(x=240,y=102)
            with open(flow_package,'w') as fp:
                fp.write('')
            fp.close()
            # 开始输入内容前需要编辑
            flow_text_normal()
            self.flow_text.insert(tkinter.END,languages_all['flow_count_start'])
            self.flow_text.see(tkinter.END)
            flow_text_disable()
            device_state = public.device_connect()
            if not device_state:
                flow_text_normal()
                self.flow_text.insert(tkinter.END,languages_all['flow_devices_null_2'])
                self.flow_text.see(tkinter.END)
                # 显示内容后立即禁用
                flow_text_disable()
            else:
                while True:
                    devices_type = open(devices_type_log, 'r').read()
                    if devices_type != 'Android':
                        print('进入Linux模式')
                        # 初始化计算总和的变量
                        rcv_total = 0
                        snd_total = 0
                        flow_text_normal()
                        self.flow_text.insert(tkinter.END, languages_all['flow_islinux'])
                        self.flow_text.see(tkinter.END)
                        self.flow_text.insert(tkinter.END, languages_all['flow_count_linux_doing'])
                        self.flow_text.see(tkinter.END)
                        flow_text_disable()
                        try:
                            while True:
                                # print('----------------------------')
                                device_state = public.device_connect()
                                device = open(devices_log, 'r').read()
                                if not device_state:
                                    flow_text_normal()
                                    self.flow_text.insert(tkinter.END, languages_all['flow_devices_suddenly_null'])
                                    self.flow_text.see(tkinter.END)
                                    flow_text_disable()
                                    break
                                if devices_type_flag or flow_stop_flag:
                                    break
                                flow_exists = self.flow_root.winfo_exists()
                                try:
                                    # 获取Linux设备的上行和下行
                                    rcv_snd1 = public.execute_cmd('adb -s ' + device + ' shell grep "wlan0" /proc/net/dev')
                                    rcv_snd_list1 = [i for i in rcv_snd1.strip().split(' ') if i != '']
                                    # print(rcv_snd_list1) # 调试查看流量值获取结果
                                    # 获取第一次的上行速度
                                    snd1 = rcv_snd_list1[9]
                                    # 获取第一次的下行速度
                                    rcv1 = rcv_snd_list1[1]
                                    time.sleep(1)
                                    rcv_snd2 = public.execute_cmd('adb -s ' + device + ' shell grep "wlan0" /proc/net/dev')
                                    rcv_snd_list2 = [i for i in rcv_snd2.strip().split(' ') if i != '']
                                    # 获取第二次的上行速度
                                    snd2 = rcv_snd_list2[9]
                                    # 获取第二次的下行速度
                                    rcv2 = rcv_snd_list2[1]
                                    snd_finally = (int(snd2) - int(snd1)) / 1024
                                    snd_finally_update = round(snd_finally, 2)
                                    rcv_finally = (int(rcv2) - int(rcv1)) / 1024
                                    rcv_finally_update = round(rcv_finally, 2)
                                    if snd_finally_update >= 1024:
                                        snd_finally_mb = snd_finally / 1024
                                        snd_finally_update_mb = round(snd_finally_mb,2)
                                        self.up_flow_str.set(str(snd_finally_update_mb) + 'MB/s')
                                    else:
                                        self.up_flow_str.set(str(snd_finally_update) + 'KB/s')
                                    if rcv_finally_update >= 1024:
                                        rcv_finally_mb = rcv_finally / 1024
                                        rcv_finally_update_mb = round(rcv_finally_mb, 2)
                                        self.down_flow_str.set(str(rcv_finally_update_mb) + 'MB/s')
                                    else:
                                        self.down_flow_str.set(str(rcv_finally_update) + 'KB/s')
                                    # 计算上行行流量值总和
                                    snd_total += snd_finally_update
                                    # print('上行流量总和：' + str(snd_total))
                                    if 1024 <= snd_total < 1048576:  # 该表达式同等 snd_total >= 1024 and snd_total < 1048576
                                        snd_total_mb = snd_total / 1024
                                        snd_finally_update_mb = round(snd_total_mb, 2)
                                        self.up_flow_total_str.set(str(round(snd_finally_update_mb, 2)) + 'MB')
                                    elif rcv_total > 1048576:
                                        snd_total_gb = snd_total / 1024 / 1024
                                        snd_finally_update_gb = round(snd_total_gb, 2)
                                        self.up_flow_total_str.set(str(round(snd_finally_update_gb, 2)) + 'GB')
                                    else:
                                        self.up_flow_total_str.set(str(round(snd_total, 2)) + 'KB')
                                    # 计算下行行流量值总和
                                    rcv_total += rcv_finally_update
                                    # print('下行流量总和：' + str(rcv_total))
                                    if 1024 <= rcv_total < 1048576:  # 该表达式同等 rcv_total >= 1024 and rcv_total < 1048576
                                        rcv_total_mb = rcv_total / 1024
                                        rcv_finally_update_mb = round(rcv_total_mb, 2)
                                        self.down_flow_total_str.set(str(round(rcv_finally_update_mb,2)) + 'MB')
                                    elif rcv_total > 1048576:
                                        rcv_total_gb = rcv_total / 1024 / 1024
                                        rcv_finally_update_gb = round(rcv_total_gb, 2)
                                        self.down_flow_total_str.set(str(round(rcv_finally_update_gb,2)) + 'GB')
                                    else:
                                        self.down_flow_total_str.set(str(round(rcv_total,2)) + 'KB')
                                    if flow_exists == 0:
                                        package_flag = False
                                        print('flow_up_down检测线程已结束！')
                                        break
                                except (UnboundLocalError,ValueError,TypeError,IndexError):
                                    pass
                        except tkinter.TclError:
                            pass
                    else:
                        print('进入安卓模式')
                        # 初始化计算总和的变量
                        rcv_total = 0
                        snd_total = 0
                        flow_text_normal()
                        self.flow_text.insert(tkinter.END, languages_all['flow_isandroid'])
                        self.flow_text.see(tkinter.END)
                        flow_text_disable()
                        flow_text_normal()
                        self.flow_text.insert(tkinter.END, languages_all['flow_find_packages_start'])
                        self.flow_text.see(tkinter.END)
                        flow_text_disable()
                        try:
                            while True:
                                # print('+++++++++++++++++++++++')
                                device_state = public.device_connect()
                                device = open(devices_log, 'r').read()
                                if not device_state:
                                    flow_text_normal()
                                    self.flow_text.insert(tkinter.END, languages_all['flow_devices_suddenly_null'])
                                    self.flow_text.see(tkinter.END)
                                    flow_text_disable()
                                    break
                                if not devices_type_flag or flow_stop_flag:
                                    break
                                if not package_flag:
                                    try:
                                        # 默认第一次必须执行
                                        pid_result = get_pid()
                                        if not pid_result:
                                            continue
                                        package_flag = True
                                    except AttributeError:
                                        continue
                                flow_exists = self.flow_root.winfo_exists()
                                try:
                                    # 获取Linux设备的上行和下行
                                    rcv_snd1 = public.execute_cmd(
                                        'adb -s ' + device + ' shell grep "wlan0" /proc/' + pid_result + '/net/dev')
                                    rcv_snd_list1 = [i for i in rcv_snd1.strip().split(' ') if i != '']
                                    # print(rcv_snd_list1) # 调试查看流量值获取结果
                                    # 获取第一次的上行速度
                                    snd1 = rcv_snd_list1[9]
                                    # 获取第一次的下行速度
                                    rcv1 = rcv_snd_list1[1]
                                    time.sleep(1)
                                    rcv_snd2 = public.execute_cmd(
                                        'adb -s ' + device + ' shell grep "wlan0" /proc/' + pid_result + '/net/dev')
                                    rcv_snd_list2 = [i for i in rcv_snd2.strip().split(' ') if i != '']
                                    # 获取第二次的上行速度
                                    snd2 = rcv_snd_list2[9]
                                    # 获取第二次的下行速度
                                    rcv2 = rcv_snd_list2[1]
                                    snd_finally = (int(snd2) - int(snd1)) / 1024
                                    snd_finally_update = round(snd_finally, 2)
                                    rcv_finally = (int(rcv2) - int(rcv1)) / 1024
                                    rcv_finally_update = round(rcv_finally, 2)
                                    if snd_finally_update >= 1024:
                                        snd_finally_mb = snd_finally / 1024
                                        snd_finally_update_mb = round(snd_finally_mb, 2)
                                        self.up_flow_str.set(str(snd_finally_update_mb) + 'MB/s')
                                    else:
                                        self.up_flow_str.set(str(snd_finally_update) + 'KB/s')
                                    if rcv_finally_update >= 1024:
                                        rcv_finally_mb = rcv_finally / 1024
                                        rcv_finally_update_mb = round(rcv_finally_mb, 2)
                                        self.down_flow_str.set(str(rcv_finally_update_mb) + 'MB/s')
                                    else:
                                        self.down_flow_str.set(str(rcv_finally_update) + 'KB/s')
                                    # 计算上行行流量值总和
                                    snd_total += snd_finally_update
                                    # print('上行流量总和：' + str(snd_total))
                                    if 1024 <= snd_total < 1048576:  # 该表达式同等 snd_total >= 1024 and snd_total < 1048576
                                        snd_total_mb = snd_total / 1024
                                        snd_finally_update_mb = round(snd_total_mb, 2)
                                        self.up_flow_total_str.set(str(round(snd_finally_update_mb, 2)) + 'MB')
                                    elif rcv_total > 1048576:
                                        snd_total_gb = snd_total / 1024 / 1024
                                        snd_finally_update_gb = round(snd_total_gb, 2)
                                        self.up_flow_total_str.set(str(round(snd_finally_update_gb, 2)) + 'GB')
                                    else:
                                        self.up_flow_total_str.set(str(round(snd_total, 2)) + 'KB')
                                    # 计算下行行流量值总和
                                    rcv_total += rcv_finally_update
                                    # print('下行流量总和：' + str(rcv_total))
                                    if 1024 <= rcv_total < 1048576:  # 该表达式同等 rcv_total >= 1024 and rcv_total < 1048576
                                        rcv_total_mb = rcv_total / 1024
                                        rcv_finally_update_mb = round(rcv_total_mb, 2)
                                        self.down_flow_total_str.set(str(round(rcv_finally_update_mb, 2)) + 'MB')
                                    elif rcv_total > 1048576:
                                        rcv_total_gb = rcv_total / 1024 / 1024
                                        rcv_finally_update_gb = round(rcv_total_gb, 2)
                                        self.down_flow_total_str.set(str(round(rcv_finally_update_gb, 2)) + 'GB')
                                    else:
                                        self.down_flow_total_str.set(str(round(rcv_total, 2)) + 'KB')
                                    if flow_exists == 0:
                                        package_flag = False
                                        print('flow_up_down检测线程已结束！')
                                        break
                                except (UnboundLocalError, ValueError, TypeError, IndexError):
                                    pass
                        except tkinter.TclError:
                            break
                    if not device_state or flow_stop_flag:
                        print('主循环结束！')
                        break
            try:
                self.start_button_disbale.place_forget()
                self.stop_button.place_forget()
                self.stopping_button_disbale.place_forget()
                self.stop_button_disbale.place(x=240,y=102)
            except tkinter.TclError:
                pass

        def t_package():
            global package_flag,devices_type_flag
            # 实时检测当前包名
            with open(flow_package,'w') as fp:
                fp.write('')
            fp.close()
            try:
                while True:
                    device_state = public.device_connect()
                    if not device_state:
                        print('设备突然断开连接，线程终止！')
                        self.up_flow_str.set('0KB/s')
                        self.down_flow_str.set('0KB/s')
                        self.up_flow_total_str.set('0')
                        self.down_flow_total_str.set('0')
                        package_flag = False
                        break
                    device = open(devices_log, 'r').read()
                    package_name = public.found_packages(device)
                    package_name_orgin = open(flow_package,'r').read()
                    device_type = open(devices_type_log,'r').read()
                    flow_exists = self.flow_root.winfo_exists()
                    time.sleep(2)
                    device_two = open(devices_log, 'r').read()
                    if package_name != package_name_orgin and package_flag and package_name_orgin != '' and package_name != ''\
                            and package_name or device != device_two:
                        if device != device_two:
                            flow_text_normal()
                            self.flow_text.insert(tkinter.END, languages_all['flow_afresh_uid'])
                            self.flow_text.see(tkinter.END)
                            flow_text_disable()
                            with open(devices_log, 'w') as fp:
                                fp.write(device_two)
                        else:
                            # 多判断防止不断重新获取包名和Uid
                            flow_text_normal()
                            self.flow_text.insert(tkinter.END, languages_all['flow_afresh_uid'])
                            self.flow_text.see(tkinter.END)
                            flow_text_disable()
                        package_flag = False
                    elif flow_exists == 0:
                        print('flow_package检测线程已结束！')
                        package_flag = False
                        try:
                            public.stop_thread(t_flow_main)
                        except ValueError:
                            pass
                        break
                    elif device_type != 'Android' and device_type != '' and devices_type_flag:
                        devices_type_flag = False
                    elif device_type == 'Android' and device_type != '' and not devices_type_flag:
                        devices_type_flag = True
                        package_flag = False
                    time.sleep(2)
            except tkinter.TclError:
                pass

        t_flow_main = threading.Thread(target=t_flow_main)
        t_flow_main.setDaemon(True)
        t_flow_main.start()

        t_package = threading.Thread(target=t_package)
        t_package.setDaemon(True)
        t_package.start()


# 获取文件MD5和大小页面
class MD5_Screen(object):
    def md5_size_form(self,md5_size_Button,md5_size_Button_disable,adb_test_disable,page_state_list,adb_test_close):
        self.md5_size_root = tkinter.Toplevel()
        self.md5_size_root.title('文件MD5大小计算工具')
        screenWidth = self.md5_size_root.winfo_screenwidth()
        screenHeight = self.md5_size_root.winfo_screenheight()
        w = 500
        h = 400
        x = (screenWidth - w) / 2
        y = (screenHeight - h) / 2
        self.md5_size_root.geometry('%dx%d+%d+%d' % (w, h, x, y))
        # self.md5_size_root.geometry('%dx%d' % (w, h))
        self.md5_size_root.iconbitmap(LOGO_path)
        self.md5_size_root.resizable(0, 0)
        self.md5_size_root.wm_attributes('-topmost', 1)

        # 初始化属性
        self.md5_size_Button = md5_size_Button
        self.md5_size_Button_disable = md5_size_Button_disable
        self.adb_test_disable = adb_test_disable
        self.adb_test_close = adb_test_close
        self.page_state_list = page_state_list

        self.page_state_list.append('md5_size')
        self.md5_size_root.protocol('WM_DELETE_WINDOW',self.close_handle)
        self.main_frame()

        return self.md5_size_root

    def close_handle(self):
        # 监听页面消失
        self.page_state_list.remove('md5_size')
        self.md5_size_Button_disable.place_forget()
        if not self.page_state_list:
            self.adb_test_disable.place_forget()
            self.adb_test_close.place(x=400, y=370)
        self.md5_size_Button.place(x=200, y=20)
        try:
            getattr(Context, "md5_size_button_disable").place_forget()
            getattr(Context, "md5_size_button").place(x=200, y=20)
        except Exception:
            pass
        self.md5_size_root.destroy()

    def count_md5_size(self,file_path):
        def t_count_md5():
            # 计算文件MD5和大小
            # self.md5_size_entry.config(state='disable')
            self.md5_size_button_disable.place(x=20, y=20)
            self.md5_clear_button_disbale.place(x=120,y=20)
            file_size_result = os.stat(file_path).st_size
            if file_size_result >= 524288000:  # 文件大于500MB的采取大文件md5获取
                self.md5_size_text.insert(tkinter.END,'\n-----------------------------')
                self.md5_size_text.insert(tkinter.END,'\n该文件大于500MB，采取分块计算，将会消耗一些时间，请耐心等待...')
                self.md5_size_text.insert(tkinter.END,'\n（计算MD5值期间不能复制粘贴修改等操作）')
                self.md5_size_text.insert(tkinter.END,'\n-----------------------------')
                self.md5_size_text.see(tkinter.END)
                self.md5_size_text.config(state='disable')
                file_md5 = public.bigger_file_md5(file_path)
                try:
                    self.md5_size_text.see(tkinter.END)
                except tkinter.TclError:
                    pass
                self.md5_size_text.config(state='normal')
                self.md5_size_text.see(tkinter.END)  # 确保能正常移动到最下方
            else:
                file_md5 = public.file_md5(file_path)
            file_size = round(file_size_result / 1024 / 1024, 2)
            self.md5_size_text.insert(tkinter.END,'\n' + file_path + '\nmd5值：' + file_md5 + '\n文件大小：' +
                                      str(file_size_result) + ' 字节 (' + str(file_size) + 'MB)')
            self.md5_size_text.insert(tkinter.END, '\n-----------------------------')
            # self.md5_size_entry.config(state='normal')
            self.md5_size_text.see(tkinter.END)
            self.md5_size_button_disable.place_forget()
            self.md5_clear_button_disbale.place_forget()

        t_count_md5 = threading.Thread(target=t_count_md5)
        t_count_md5.setDaemon(True)
        t_count_md5.start()

    def windnd_hook_files(self,widget):
        # 拖拽文件到text文本框获取文件路径功能（windnd） widget是控件
        def dragged_files(files):
            path_msg = '\n'.join((item.decode('gbk') for item in files))
            print('获取的文件路径：' + path_msg)
            # 拖拽到框即可获取md5和大小
            self.count_md5_size(path_msg)

        # 使用windnd方法
        windnd.hook_dropfiles(widget,func=dragged_files)

    def main_frame(self):
        # 上传文件获取路径
        self.md5_size_str = tkinter.StringVar()
        # self.md5_size_entry = tkinter.Entry(self.md5_size_root,width=50, highlightcolor='#87CEFA',
        #                                     textvariable=self.md5_size_str, highlightthickness=5)
        # self.md5_size_entry.place(x=20,y=20)
        # if not os.path.exists(md5_size_path):
        #     with open(md5_size_path, 'w') as fp:
        #         fp.write('')
        # path_msg = open(md5_size_path, 'r').read()
        # self.md5_size_str.set(path_msg)

        # 浏览文件
        self.md5_size_button = tkinter.Button(self.md5_size_root,text='浏览',width=10)
        self.md5_size_button.bind('<Button-1>',lambda x:self.md5_size_button_bind())
        self.md5_size_button_disable = tkinter.Button(self.md5_size_root,text='浏览',width=10)
        self.md5_size_button_disable.config(state='disable')
        self.md5_size_button.place(x=20,y=20)

        # 清空记录
        self.md5_clear_button = tkinter.Button(self.md5_size_root, text='清空', width=10)
        self.md5_clear_button_disbale = tkinter.Button(self.md5_size_root, text='清空', width=10)
        self.md5_clear_button.bind('<Button-1>', lambda x: self.md5_clear())
        self.md5_clear_button_disbale.config(state='disable')
        self.md5_clear_button.place(x=120, y=20)

        # 文件MD5和大小显示
        self.md5_size_frame = tkinter.Frame(self.md5_size_root, width=500, height=200)
        self.md5_size_scrollbar = tkinter.Scrollbar(self.md5_size_frame)
        self.md5_size_text = tkinter.Text(self.md5_size_frame, yscrollcommand=(self.md5_size_scrollbar.set), width=53, height=15,
                                      font=('宋体', 13))
        self.md5_size_scrollbar.config(command=self.md5_size_text.yview)
        self.md5_size_scrollbar.pack(side=(tkinter.RIGHT), fill=(tkinter.Y))
        # self.md5_size_text.config(state='disable')  # 设为disable防止Text多行文本框能被点击
        self.md5_size_text.pack()
        self.md5_size_text.insert(tkinter.END, '请点击浏览文件，打开后自动计算文件MD5和大小！\n温馨提示：可把文件拖拽到这里获取文件MD5和大小哦！')
        self.md5_size_frame.place(y=70)
        self.windnd_hook_files(self.md5_size_text)

    def md5_clear(self):
        # 清空显示的MD5和大小（避免记录过多）
        def t_md5_clear():
            self.md5_clear_button_disbale.place(x=120,y=20)
            self.md5_size_text.delete(0.0,tkinter.END)
            self.md5_size_text.insert(tkinter.END, '请点击浏览文件，打开后自动计算文件MD5和大小！\n温馨提示：可把文件拖拽到这里获取文件MD5和大小哦！')
            self.md5_clear_button_disbale.place_forget()

        t_md5_clear = threading.Thread(target=t_md5_clear)
        t_md5_clear.setDaemon(True)
        t_md5_clear.start()

    def md5_size_button_bind(self):
        def t_md5_size_button():
            # 浏览文件（不限类型）
            self.md5_size_button_disable.place(x=20,y=20)
            # self.md5_size_entry.config(state='disable')
            if not os.path.exists(md5_size_path):
                with open(md5_size_path,'w') as fp:
                    fp.write('C:\\Users\\' + getpass.getuser() + '\\Desktop\\')
                fp.close()
            path_msg = open(md5_size_path, 'r').read()
            path_msg_finally = '\\'.join(path_msg.split('\\')[:-1])
            dlg = win32ui.CreateFileDialog(True, "csv", None, 0x04 | 0x02)
            dlg.SetOFNInitialDir(path_msg_finally)
            self.md5_size_root.wm_attributes('-topmost', 0)
            dlg.DoModal()  # 显示文件选择框
            file_path = dlg.GetPathName()  # 获取选择的文件名称
            if file_path == '':
                # self.md5_size_entry.config(state='normal')
                # self.md5_size_text.config(state='normal')
                self.md5_size_button_disable.place_forget()
                pass
            else:
                self.md5_size_str.set(file_path)
                with open(md5_size_path, 'w') as fp:
                    fp.write(file_path)
                fp.close()
                self.count_md5_size(file_path)  # 记录文件MD5和大小
            self.md5_size_root.wm_attributes('-topmost', 1)

        t_md5_size_button = threading.Thread(target=t_md5_size_button)
        t_md5_size_button.setDaemon(True)
        t_md5_size_button.start()


class AdbShell(object):
    def adb_shell_cmd(self,customize_frame1,width_button,adb_test_disable,page_state_list,adb_test_close):
        # UI布局
        self.page_state_list = page_state_list
        self.adb_shell_button = tkinter.Button(customize_frame1,text='adb shell终端',width=width_button)
        self.adb_shell_button_disable = tkinter.Button(customize_frame1,text='正在使用终端...',width=width_button)
        self.adb_shell_button_disable.config(state='disable')
        self.adb_shell_button.bind('<Button-1>',lambda x:self.adb_shell_cmd_bind(adb_test_disable,adb_test_close))
        self.adb_shell_button.place(x=200, y=60)
        if 'adb_shell' in page_state_list:
            self.adb_shell_button.place_forget()
            self.adb_shell_button_disable.place(x=200, y=60)
            setattr(Context,"adb_shell_button",self.adb_shell_button)
            setattr(Context,"adb_shell_button_disable",self.adb_shell_button_disable)

    def adb_shell_cmd_bind(self,adb_test_disable,adb_test_close):
        def t_adb_shell_cmd_bind():
            # 启动adb shell终端
            self.page_state_list.append('adb_shell')
            self.adb_shell_button.place_forget()
            self.adb_shell_button_disable.place(x=200, y=60)
            adb_test_disable.place(x=400, y=370)
            device_state = public.device_connect()
            if not device_state:
                tkinter.messagebox.showwarning('打开终端失败','请连接设备后再打开')
            else:
                device_SN = open(devices_log,'r').read()
                # 弹出cmd窗口并执行以下命令
                os.system(f'adb -s {device_SN} shell')
            self.page_state_list.remove('adb_shell')
            self.adb_shell_button_disable.place_forget()
            self.adb_shell_button.place(x=200, y=60)

            try:
                getattr(Context, "adb_shell_button_disable").place_forget()
                getattr(Context, "adb_shell_button").place(x=200, y=60)
            except Exception:
                pass
            if not self.page_state_list:
                adb_test_disable.place_forget()
                adb_test_close.place(x=400, y=370)

        t_adb_shell_cmd_bind = threading.Thread(target=t_adb_shell_cmd_bind)
        t_adb_shell_cmd_bind.setDaemon(True)
        t_adb_shell_cmd_bind.start()


class PushPull(object):
    def push_pull_form(self,customize_frame1,width_button,adb_test_disable,page_state_list,adb_test_close):
        # push_pull UI 布局
        self.page_state_list = page_state_list
        self.push_pull_button = tkinter.Button(customize_frame1,text='上传Push下载Pull',width=width_button)
        self.push_pull_button_disable = tkinter.Button(customize_frame1,text='上传Push下载Pull',width=width_button)
        self.push_pull_button_disable.config(state='disable')
        self.push_pull_button.place(x=20,y=100)
        self.push_pull_button.bind('<Button-1>',lambda x:self.push_pull_main_form(adb_test_disable,adb_test_close))

        if 'push_pull' in page_state_list:
            self.push_pull_button.place_forget()
            self.push_pull_button_disable.place(x=20, y=90)
            setattr(Context,"adb_shell_button",self.push_pull_button)
            setattr(Context,"push_pull_button_disable",self.push_pull_button_disable)

    def push_pull_main_form(self,adb_test_disable,adb_test_close):
        self.push_pull_root = tkinter.Toplevel()
        self.push_pull_root.title('上传push下载pull工具')
        screenWidth = self.push_pull_root.winfo_screenwidth()
        screenHeight = self.push_pull_root.winfo_screenheight()
        w = 580
        h = 300
        x = (screenWidth - w) / 2
        y = (screenHeight - h) / 2
        self.push_pull_root.geometry('%dx%d+%d+%d' % (w, h, x, y))
        # self.md5_size_root.geometry('%dx%d' % (w, h))
        self.push_pull_root.iconbitmap(LOGO_path)
        self.push_pull_root.resizable(0, 0)
        # self.push_pull_root.wm_attributes('-topmost', 1)

        self.push_pull_button.place_forget()
        self.push_pull_button_disable.place(x=20, y=100)
        self.adb_test_disable = adb_test_disable
        self.adb_test_close = adb_test_close
        self.adb_test_disable.place(x=400, y=370)
        self.page_state_list.append('push_pull')

        self.push_pull_root.protocol('WM_DELETE_WINDOW', self.close_handle)
        self.push_main_frame()
        self.push_frame_bind()

        self.push_pull_root.mainloop()

        return self.push_pull_root

    def close_handle(self):
        # 监听页面消失
        self.page_state_list.remove('push_pull')
        self.push_pull_button_disable.place_forget()
        if not self.page_state_list:
            self.adb_test_disable.place_forget()
            self.adb_test_close.place(x=400, y=370)
        self.push_pull_button.place(x=20, y=100)
        try:
            getattr(Context, "push_pull_button_disable").place_forget()
            getattr(Context, "push_pull_button").place(x=20, y=100)
        except Exception:
            pass
        self.push_pull_root.destroy()

    def push_main_frame(self):
        # 显示状态
        self.push_pull_state_str = tkinter.StringVar()
        self.push_pull_state_label = tkinter.Label(self.push_pull_root, textvariable=self.push_pull_state_str, bg='black',
                                                fg='#ffffff', width=30, height=2)
        self.push_pull_state_str.set('此处显示上传和下载状态')
        self.push_pull_state_label.place(x=190, y=20)

        # Push 单选
        self.push_state_str = tkinter.IntVar()
        self.push_radio = tkinter.Radiobutton(self.push_pull_root,text='Push上传',variable=self.push_state_str,value=1)
        self.push_radio.place(x=190,y=65)
        self.push_state_str.set(1)
        self.push_radio.bind('<Button-1>',lambda x:self.push_radio_handle())

        # Pull 单选，默认不选中
        self.pull_state_str = tkinter.IntVar()
        self.pull_radio = tkinter.Radiobutton(self.push_pull_root,text='Pull下载',variable=self.pull_state_str,value=1)
        self.pull_state_str.set(0)  # 默认不选中
        self.pull_radio.place(x=330,y=65)
        self.pull_radio.bind('<Button-1>',lambda x:self.pull_radio_handle())

    def push_radio_handle(self):
        # push_radio_handle
        self.push_state_str.set(1)
        self.pull_state_str.set(0)
        self.pull_frame.place_forget()
        self.push_frame_bind()

    def pull_radio_handle(self):
        # pull_radio_handle
        self.push_state_str.set(0)
        self.pull_state_str.set(1)
        self.push_frame.place_forget()
        self.pull_frame_bind()

    def push_frame_bind(self):
        global push_pull_flag
        push_pull_flag = False
        # push frame
        self.push_frame = tkinter.Frame(self.push_pull_root,width=580,height=150)
        # 本地路径label
        self.push_local_label = tkinter.Label(self.push_frame, text='请输入要上传的本地文件路径（包含文件夹）：', fg='red')
        self.push_local_label.place(x=20, y=5)

        # 本地输入框
        self.push_str = tkinter.StringVar()
        self.push_entry = tkinter.Entry(self.push_frame, textvariable=self.push_str,
                                                  width=55, highlightcolor='blue', highlightthickness=1)
        self.push_entry.place(x=20, y=33)
        if not os.path.exists(push_local_path1):
            with open(push_local_path1,'w') as fp:
                fp.write('')
        push_local_str = open(push_local_path1,'r',encoding='utf-8',errors='ignore').read()
        self.push_str.set(push_local_str)

        # 浏览文件按钮
        self.push_browse_button = tkinter.Button(self.push_frame, text='浏览文件')
        self.push_browse_button_disable = tkinter.Button(self.push_frame, text='浏览文件')
        self.push_browse_button_disable.config(state='disable')
        self.push_browse_button.place(x=420, y=29)
        self.push_browse_button.bind('<Button-1>', lambda x: self.push_local_path_bind(push_pull_flag))

        # 浏览文件夹按钮
        self.push_browse_dir_button = tkinter.Button(self.push_frame, text='浏览文件夹')
        self.push_browse_dir_button_disable = tkinter.Button(self.push_frame, text='浏览文件夹')
        self.push_browse_dir_button_disable.config(state='disable')
        self.push_browse_dir_button.place(x=480, y=29)
        self.push_browse_dir_button.bind('<Button-1>', lambda x: self.push_local_dir_bind(push_pull_flag))

        # 设备空间label
        self.push_device_label = tkinter.Label(self.push_frame, text='请输入要上传到设备的具体路径：', fg='red')
        self.push_device_label.place(x=20, y=60)

        # 设备路径输入框
        self.push_device_str = tkinter.StringVar()
        self.push_device_entry = tkinter.Entry(self.push_frame, textvariable=self.push_device_str,
                                        width=55, highlightcolor='blue', highlightthickness=1)
        self.push_device_entry.place(x=20, y=90)
        if not os.path.exists(push_device_path1):
            with open(push_device_path1,'w') as fp:
                fp.write('')

        push_device_str = open(push_device_path1, 'r').read()
        self.push_device_str.set(push_device_str)

        self.push_frame.place(y=90)

        # 进度条
        # self.progressbarOne = tkinter.ttk.Progressbar(self.push_pull_root, length=450)
        # self.progressbarOne['maximum'] = 100
        # self.progressbarOne['value'] = 0
        # self.progressbarOne.place(x=20, y=220)

        # Push上传按钮
        self.push_button = tkinter.Button(self.push_frame, text='Push上传',width=20)
        self.push_button_disable = tkinter.Button(self.push_frame, text='Push上传',width=20)
        self.push_button_disable.config(state='disable')
        self.push_button.place(x=220, y=120)
        self.push_button.bind('<Button-1>', lambda x: self.push_bind(push_pull_flag))

    def pull_frame_bind(self):
        global push_pull_flag
        push_pull_flag = True
        # pull frame
        self.pull_frame = tkinter.Frame(self.push_pull_root,width=580,height=150)
        # 本地路径label
        self.pull_local_label = tkinter.Label(self.pull_frame, text='请输入要下载到本地的目录路径：', fg='red')
        self.pull_local_label.place(x=20, y=60)

        # 本地输入框
        self.pull_str = tkinter.StringVar()
        self.pull_entry = tkinter.Entry(self.pull_frame, textvariable=self.pull_str,
                                                  width=55, highlightcolor='blue', highlightthickness=1)
        self.pull_entry.place(x=20, y=90)
        if not os.path.exists(pull_local_path1):
            with open(pull_local_path1,'w') as fp:
                fp.write('')
        pull_local_str = open(pull_local_path1,'r',encoding='utf-8',errors='ignore').read()
        self.pull_str.set(pull_local_str)

        # 浏览文件夹按钮
        self.pull_browse_dir_button = tkinter.Button(self.pull_frame, text='浏览文件夹')
        self.pull_browse_dir_button_disable = tkinter.Button(self.pull_frame, text='浏览文件夹')
        self.pull_browse_dir_button_disable.config(state='disable')
        self.pull_browse_dir_button.place(x=420, y=86)
        self.pull_browse_dir_button.bind('<Button-1>', lambda x: self.push_local_dir_bind(push_pull_flag))

        # 设备空间label
        self.pull_device_label = tkinter.Label(self.pull_frame, text='请输入设备需要下载到本地的文件路径（包含文件夹）：', fg='red')
        self.pull_device_label.place(x=20, y=5)

        # 设备路径输入框
        self.pull_device_str = tkinter.StringVar()
        self.pull_device_entry = tkinter.Entry(self.pull_frame, textvariable=self.pull_device_str,
                                        width=55, highlightcolor='blue', highlightthickness=1)
        self.pull_device_entry.place(x=20, y=33)
        if not os.path.exists(pull_device_path1):
            with open(pull_device_path1,'w') as fp:
                fp.write('')

        pull_device_str = open(pull_device_path1, 'r').read()
        self.pull_device_str.set(pull_device_str)

        self.pull_frame.place(y=90)

        # Pull下载按钮
        self.pull_button = tkinter.Button(self.pull_frame, text='Pull下载',width=20)
        self.pull_button_disable = tkinter.Button(self.pull_frame, text='Pull下载',width=20)
        self.pull_button_disable.config(state='disabled')
        self.pull_button.place(x=220, y=120)
        self.pull_button.bind('<Button-1>', lambda x: self.push_bind(push_pull_flag))

    def push_local_path_bind(self,push_pull_flag):
        def t_push_local_path():
            # 获取日志路径
            self.push_browse_button.place_forget()
            self.push_browse_button_disable.place(x=420, y=29)
            self.push_browse_dir_button.place_forget()
            self.push_browse_dir_button_disable.place(x=480, y=29)
            if not push_pull_flag:
                push_local_path = public.browse_bind(True)
                with open(push_local_path1,'w',encoding='utf-8',errors='ignore') as fp:
                    fp.write(push_local_path)
                self.push_entry.delete(0,tkinter.END)
                self.push_entry.insert(tkinter.END,push_local_path)
            else:
                pull_local_path = public.browse_bind(True)
                with open(pull_local_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                    fp.write(pull_local_path)
                self.pull_entry.delete(0, tkinter.END)
                self.pull_entry.insert(tkinter.END, pull_local_path)
            self.push_browse_button_disable.place_forget()
            self.push_browse_button.place(x=420, y=29)
            self.push_browse_dir_button_disable.place_forget()
            self.push_browse_dir_button.place(x=480, y=29)

        t_push_local_path = threading.Thread(target=t_push_local_path)
        t_push_local_path.setDaemon(True)
        t_push_local_path.start()

    def push_local_dir_bind(self,push_pull_flag):
        def t_push_local_dir():
            # 获取日志文件夹路径
            if not push_pull_flag:
                self.push_browse_button.place_forget()
                self.push_browse_button_disable.place(x=420, y=29)
                self.push_browse_dir_button.place_forget()
                self.push_browse_dir_button_disable.place(x=480, y=29)
            else:
                self.pull_browse_dir_button.place_forget()
                self.pull_browse_dir_button_disable.place(x=420, y=86)
            if not push_pull_flag:
                # 打开文件夹对话框
                push_local_path = public.brower_folder(self.push_entry.get())
                if push_local_path.strip() != '':
                    with open(push_local_path1,'w',encoding='utf-8',errors='ignore') as fp:
                        fp.write(push_local_path)
                    self.push_entry.delete(0,tkinter.END)
                    self.push_entry.insert(tkinter.END,push_local_path)
            else:
                pull_local_path = public.brower_folder(self.push_entry.get())
                if pull_local_path.strip() != '':
                    with open(pull_local_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                        fp.write(pull_local_path)
                    self.pull_entry.delete(0, tkinter.END)
                    self.pull_entry.insert(tkinter.END, pull_local_path)
            if not push_pull_flag:
                self.push_browse_button_disable.place_forget()
                self.push_browse_button.place(x=420, y=29)
                self.push_browse_dir_button_disable.place_forget()
                self.push_browse_dir_button.place(x=480, y=29)
            else:
                self.pull_browse_dir_button_disable.place_forget()
                self.pull_browse_dir_button.place(x=420, y=86)

        t_push_local_dir = threading.Thread(target=t_push_local_dir)
        t_push_local_dir.setDaemon(True)
        t_push_local_dir.start()

    def push_bind(self,push_pull_flag):
        def t_push_bind():
            # push文件主逻辑
            if not push_pull_flag:
                self.push_button.place_forget()
                self.push_button_disable.place(x=220, y=120)
            else:
                self.pull_button.place_forget()
                self.pull_button_disable.place(x=220, y=120)
            device_state = public.device_connect()
            if not device_state:
                self.push_pull_state_str.set('设备未连接\n请连接设备后重试')
            else:
                if not push_pull_flag:
                    # 获取本地路径
                    push_local_path = self.push_entry.get()
                    # 获取设备路径
                    push_device_path = self.push_device_entry.get()
                    # 使用adb push命令上传文件到设备中
                    if push_local_path and push_device_path:
                        # 判断本地路径是否存在
                        if not os.path.exists(push_local_path):
                            tkinter.messagebox.showerror('错误', '本地路径不存在，请重新输入')
                        else:
                            # 判断本地路径和设备路径含有中文，有中文就提示，没有（纯英文路径）就上传
                            if public.contains_chinese(push_local_path) or public.contains_chinese(push_device_path):
                                tkinter.messagebox.showerror('错误',
                                                             '本地路径或设备路径含有中文(仅支持全英文路径)，请重新输入')
                            else:
                                # self.progressbarOne['value'] = 0  # 重置进度条
                                # 获取本地路径最后一个文件名或文件夹名
                                # push_local_path_name = os.path.basename(push_local_path)

                                # # 判断本地路径是否为文件夹
                                # if os.path.isdir(push_local_path):
                                #     # 获取本地路径文件夹的总大小，同时包含子文件夹和文件的大小
                                #     push_local_path_size = 0
                                #     for root, dirs, files in os.walk(push_local_path):
                                #         for name in files:
                                #              push_local_path_size += os.path.getsize(os.path.join(root, name))
                                #     print('push_local_path_size:',push_local_path_size)
                                #
                                #     command = f'adb push "{push_local_path}" {push_device_path}'
                                #     p = subprocess.Popen(command, shell=False, stdout=(subprocess.PIPE),stderr=(subprocess.STDOUT))
                                #     while p.poll() is None:
                                #         # 获取adb push命令执行过程中的输出的大小
                                #         push_file_size = public.linux_sn_cmd('adb shell du ' + os.path.join(push_device_path,push_local_path_name)).split('\t')[0]
                                #         # print('正在上传的文件大小 push_file_size:',push_file_size)
                                #         # 计算上传进度
                                #         push_progress = int(push_file_size) / int(push_local_path_size) * 100
                                #         print('push上传进度：',push_progress)
                                #         self.progressbarOne['value'] = push_progress
                                #         self.push_pull_state_str.set(f'文件上传中...\n进度为{push_progress}%')
                                # else:
                                #     # 首先获取本地路径文件大小
                                #     push_local_path_size = os.path.getsize(push_local_path)
                                #     print('push_local_path_size:',push_local_path_size)
                                #     # result = public.execute_cmd(f'adb push "{push_local_path}" {push_device_path}')
                                #     # print('push上传结果：',result)
                                #
                                #     command = f'adb push "{push_local_path}" {push_device_path}'
                                #     p = subprocess.Popen(command, shell=False, stdout=(subprocess.PIPE), stderr=(subprocess.STDOUT))
                                #     while p.poll() is None:
                                #         # 获取adb push命令执行过程中的输出的大小
                                #         push_file_size = public.linux_sn_cmd('adb shell ls -l ' + os.path.join(push_device_path,push_local_path_name)).split(' ')[4]
                                #         print('正在上传的文件大小 push_file_size:',push_file_size)
                                #         # 计算上传进度
                                #         # push_progress = round((int(push_file_size) / int(push_local_path_size)) * 100, 2)
                                #         push_progress = push_progress = int(push_file_size) / int(push_local_path_size) * 100
                                #         # print('push上传进度：',push_progress)
                                #         self.progressbarOne['value'] = push_progress
                                #         self.push_pull_state_str.set(f'文件上传中...\n进度为{push_progress}%')

                                # 进度条
                                # self.progressbarOne['value'] = 100

                                # 获取当前设备序列号
                                # device = open(devices_log,'r').read()
                                if not push_pull_flag:
                                    # 保存本地路径和设备路径到文件中，方便下次使用
                                    with open(push_local_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                                        fp.write(push_local_path)
                                    with open(push_device_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                                        fp.write(push_device_path)
                                    # 使用os.system()执行adb push命令更加稳定，更加高效
                                    # os.system(f'start cmd.exe /K adb -s {device} push "{push_local_path}" {push_device_path}')
                                    os.system(f'start cmd.exe /K adb push "{push_local_path}" {push_device_path}')
                else:
                    # 获取本地路径
                    pull_local_path = self.pull_entry.get()
                    # 获取设备路径
                    pull_device_path = self.pull_device_entry.get()

                    if pull_local_path and pull_device_path:
                        # 判断本地路径是否存在
                        if not os.path.exists(pull_local_path):
                            tkinter.messagebox.showerror('错误', '本地路径不存在，请重新输入')
                        else:
                            # 判断本地路径和设备路径含有中文，有中文就提示，没有（纯英文路径）就上传
                            if public.contains_chinese(pull_local_path) or public.contains_chinese(pull_device_path):
                                tkinter.messagebox.showerror('错误','本地路径或设备路径含有中文(仅支持全英文路径)，请重新输入')
                            else:
                                with open(pull_local_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                                    fp.write(pull_local_path)
                                with open(pull_device_path1, 'w', encoding='utf-8', errors='ignore') as fp:
                                    fp.write(pull_device_path)
                                os.system(f'start cmd.exe /K adb pull "{pull_device_path}" {pull_local_path}')

                self.push_pull_state_str.set('成功启动push任务\n可创建多个任务同时push')

            time.sleep(2)
            if not push_pull_flag:
                self.push_button_disable.place_forget()
                self.push_button.place(x=220, y=120)
            else:
                self.pull_button_disable.place_forget()
                self.pull_button.place(x=220, y=120)

        t_push_bind = threading.Thread(target=t_push_bind)
        t_push_bind.setDaemon(True)
        t_push_bind.start()

